﻿Imports System.Data.OleDb

Public Class AddQuiz
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try

        
        Dim myTags As TagsManagement = New TagsManagement()
        Dim dr As OleDbDataReader = myTags.SelectAllTagsVisible()

        Dim availableTags As String = ""

        If dr.HasRows Then
            Do While dr.Read()
                If Not IsDBNull(dr("TagName")) Then availableTags = availableTags + (dr("TagName")).ToString() + ","
            Loop
        End If

        hdnDBTags.Value = availableTags
        Catch ex As Exception

        End Try
    End Sub
    Public Sub btnCreate_Quiz(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreate.Click
        Try

        
        If (hdnSelectedTags.Value <> "") Then


            Dim myGuid As New Guid
            myGuid = Guid.NewGuid()

            Dim fileIsAnImage As Boolean = False
            Dim fileName As String
            Dim myFileName As String = String.Empty

            If (fileUploadPicture.HasFile) Then

                fileName = Server.HtmlEncode(fileUploadPicture.FileName)
                Dim extension As String = System.IO.Path.GetExtension(fileName)

                If (extension = ".jpg") Then
                    myFileName = myGuid.ToString() + ".jpg"
                    fileIsAnImage = True
                ElseIf (extension = ".png") Then
                    myFileName = myGuid.ToString() + ".png"
                    fileIsAnImage = True
                End If


                If (fileIsAnImage) Then
                    'check and add tags
                    Dim writtenTags As String = hdnSelectedTags.Value
                    Dim arrayOfWrittenTags As IList(Of String) = writtenTags.Split(",").ToList()

                    Dim myTags As TagsManagement = New TagsManagement()
                    Dim dr As OleDbDataReader = myTags.SelectAllTagsVisible()

                    Dim availableTags As ArrayList = New ArrayList()

                    If dr.HasRows Then
                        Do While dr.Read()
                            If Not IsDBNull(dr("TagName")) Then availableTags.Add((dr("TagName")))
                        Loop
                    End If

                    Dim aCookie As HttpCookie = Request.Cookies("userIdCookie")
                    Dim myuserId = Convert.ToInt32(aCookie.Value)

                    For index = 0 To arrayOfWrittenTags.Count - 1
                        'add new tag to tag table
                        If (Not availableTags.Contains(arrayOfWrittenTags(index))) Then
                            Dim addTag As TagsManagement = New TagsManagement()

                            addTag.InsertTags(myuserId, arrayOfWrittenTags(index))

                            'add ranking points
                            Dim myPoints As Users_RankingPointsManagement = New Users_RankingPointsManagement()
                            Dim rankingPointsId As Int32 = myPoints.SelectRankingPointsByType("Add Tag")

                            myPoints.InsertUsersRankingPoints(myuserId, rankingPointsId)

                        End If

                    Next



                    'create picture path
                    Dim filePath = Request.MapPath("../Pictures/Quiz") + "/" + myFileName
                    fileUploadPicture.SaveAs(filePath)

                    'get userID by username
                    Dim userId As Int32 = New aspnet_UsersManagement().SelectUserIDByUsername(User.Identity.Name)
                    Dim quizInsert As QuizManagement = New QuizManagement()
                    Dim currentDate = New DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day)
                    quizInsert.InsertQuiz(userId, txtName.Text, txtDescription.Text, currentDate, "Pictures/Quiz/" + myFileName)


                    'get latestQuizIdInserted
                    Dim QuizID As Int32 = New QuizManagement().SelectTopQuizID()


                    'add quiz tags

                    For index = 0 To arrayOfWrittenTags.Count - 1
                        'add new tag to tag table
                        Dim currentTag As String = arrayOfWrittenTags(index).ToString()

                        Dim addTag As TagsManagement = New TagsManagement()
                        Dim tagId As Int32 = addTag.SelectTagIdByTagName(currentTag)

                        addTag.InsertQuizTag(QuizID, tagId)


                    Next

                    'add pagename to pageNames

                    Dim pageName As PagesNamesManagement = New PagesNamesManagement()
                    pageName.InsertPageName("QuizOverview.aspx?quizId=" + QuizID.ToString())
                    pageName.InsertPageName("PlayQuiz.aspx?quizId=" + QuizID.ToString())

                    'transfer to add questions

                    Dim _contextQuizID As HttpContext = HttpContext.Current
                    _contextQuizID.Items.Add("QuizId", QuizID)
                    Server.Transfer("AddQuizQuestions.aspx")


                Else
                    lblMessage.Text = "Only jpeg or png files are allowed"
                End If
            Else


                lblMessage.Text = "Image is required to create a Quiz"

            End If

        Else

            lblMessage.Text = "One or more tags are required to create a Quiz"
        End If
        Catch ex As Exception

        End Try
    End Sub
End Class